#!/usr/bin/env python

import csv
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
import sys


if len(sys.argv) != 2:
    print "please input the feedback dump file"
    sys.exit(-1);

times = []
rtp_ts = []
arrival = []
size = []
ts_delta = []
t_delta = []
t_ts_delta = []
avg = 0
avg_t_ts_delta = []
size_delta = []
bps = []

with open(sys.argv[1]) as input:
    reader = csv.reader(input, delimiter=',')
    next(reader)
    for row in reader:
        print row
        times.append(long(row[2]))
        rtp_ts.append(long(row[1]))
        arrival.append(long(row[2]))
        size.append(long(row[3]))
        ts_delta.append(long(row[4]))
        t_delta.append(long(row[5]))
        t_ts_delta.append(long(row[5])-long(row[4]))
        if avg == 0:
            avg = long(row[5])-long(row[4])
        else:
            avg = avg * 0.95 + (long(row[5])-long(row[4])) * 0.05
        
        avg_t_ts_delta.append(avg)
        size_delta.append(long(row[6]))
        bps.append(long(row[7]))

hist_res = [200, 80]
hist_cmap = 'Blues'
plt.figure(1)
ax1 = plt.subplot(8,1,1)
ax1.scatter(times, rtp_ts,  label='rtp-ts')
ax1.legend()
print ax1
plt.ylabel('rtp-ts')
ax1.grid(True)
ax2 = plt.subplot(8,1,2)
ax2.scatter(times, arrival,  label='arrival')
ax2.legend()
plt.ylabel('arrival')
ax2.grid(True)
ax3 = plt.subplot(8,1,3)
ax3.scatter(times, size,  label='size')
ax3.legend()
plt.ylabel('size')
ax3.grid(True)
ax4 = plt.subplot(8,1,4)
ax4.scatter(times, ts_delta,  label='ts-delta')
ax4.legend()
plt.ylabel('ts-delta')
ax4.grid(True)
ax5 = plt.subplot(8,1,5)
ax5.scatter(times, t_delta,  label='t-delta')
ax5.legend()
plt.ylabel('t-delta')
ax5.grid(True)
ax6 = plt.subplot(8,1,6)
ax6.scatter(times, size_delta,  label='size-delta')
ax6.legend()
plt.ylabel('size-delta')
ax6.grid(True)
ax7 = plt.subplot(8,1,7)
ax7.scatter(times, bps,  label='bps')
ax7.legend()
plt.ylabel('bps')
ax7.grid(True)
ax8 = plt.subplot(8,1,8)
ax8.scatter(times, t_ts_delta,  label='t-ts-delta')
ax8.plot(times, avg_t_ts_delta, 'r--', label='avg')
ax8.legend()
plt.ylabel('t-ts-delta')
ax8.grid(True)
plt.show()
#
# plt.figure(1)
#
# ax1 = plt.subplot(4,1,1)
# ax1t = ax1.twinx()
#
# ax1.plot(times, offsets0, label='offset')
# ax1.plot(times, thresholds0, 'r--', label='threshold')
# ax1t.plot(times, var_noises0, 'm:', label='var-noise')
#
# ax1.legend()
# ax1.grid(True)
#
# ax2 = plt.subplot(4,1,2)
# ax2t = ax2.twinx()
#
# ax2.plot(times, offsets1, label='offset')
# ax2.plot(times, thresholds1, 'r--', label='threshold')
# ax2t.plot(times, var_noises1, 'm:', label='var-noise')
#
# ax2.legend()
# ax2t.legend()
# ax2.grid(True)
#
#
# ax3 = plt.subplot(4,1,3)
# incoming_line, = ax3.plot(times, incomings, label='incoming')
# target_line, = ax3.plot(times, targets, 'r--', label='target' )
# ax3.legend()
# ax3.grid(True)
#
# ax4 = plt.subplot(4,1,4)
# ax4.scatter(times, usages, label='usage')
# ax4.legend()
#
#
# #plt.subplot(4,1,3)
# #plt.plot(ests)
# #plt.ylabel('est (kbps)')
# #plt.scatter(dss, dts)
#
# #plt.subplot(4,1,4)
# #plt.scatter(dss, dts, hist_res)
#
# print 'show'
# plt.show()
